<?php

namespace App\Model;

use EasySwoole\DDL\Enum\Character;
use EasySwoole\DDL\Enum\Engine;
use EasySwoole\ORM\AbstractModel;
use EasySwoole\ORM\Exception\Exception;
use EasySwoole\ORM\Utility\Schema\Table;
use Throwable;

class TradePropertyModel extends AbstractModel
{
    protected $tableName = 't_trade_property';
    protected $autoTimeStamp = true; // 自动写入create_time和update_time 这两个字段是int

    /**
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getAccount($where)
    {
        $where['is_deleted'] = 0;
        return $this->order('id', 'desc')->get($where);
    }

    /**
     * @throws Throwable
     * @throws Exception
     */
    public function addAccount($where)
    {
        $this->data($where, false);
        return $this->save();
    }

    public function schemaInfo(bool $isCache = true): Table
    {
        $table = new Table($this->tableName);

        $table->setIfNotExists();
        $table->setTableEngine(Engine::INNODB);
        $table->setTableCharset(Character::UTF8MB4_GENERAL_CI);
        $table->setTableComment('用户缴纳物业费表');

        $table->colInt('id')->setIsAutoIncrement()->setIsPrimaryKey(true)->setColumnComment("ID");
        $table->colInt('uid')->setDefaultValue(0)->setIsNotNull()->setColumnComment('用户id');
        $table->colInt('hid')->setDefaultValue(0)->setIsNotNull()->setColumnComment('房屋id');
        $table->colVarChar('order_no', 32)->setDefaultValue('')->setColumnComment('订单号');
        $table->colVarChar('code', 32)->setDefaultValue('')->setColumnComment('唯一标识');
        $table->colDecimal('price',10,2)->setDefaultValue(0.00)->setColumnComment('住宅物业基础费用费');
        $table->colDecimal('public_price',10,2)->setDefaultValue(0.00)->setColumnComment('公共区域公摊电费');
        $table->colDecimal('rubbish_price',10,2)->setDefaultValue(0.00)->setColumnComment('公共垃圾费用');
        $table->colDecimal('total_price',10,2)->setDefaultValue(0.00)->setColumnComment('物业费总计');
        $table->colInt('status', 2)->setDefaultValue(0)->setColumnComment('订单状态：(0未支付,1已支付)');
        $table->colTinyInt('is_deleted', 1)->setDefaultValue(0)->setIsNotNull()->setColumnComment('是否删除：0.未删除，1.已删除');
        $table->colInt('create_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('创建时间戳');
        $table->colInt('update_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('更新时间戳');

        $table->indexNormal('uid_index', 'uid');
        $table->indexNormal('hid_index', 'hid');
        $table->indexNormal('order_no_index', 'order_no');
        $table->indexNormal('total_price_index', 'total_price');
        $table->indexNormal('code_index', 'code');

        return $table;
    }
}